package com.jueming.cashbook.common.response;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jueming.cashbook.core.constans.Constans;
import com.jueming.cashbook.core.response.ResultCode;

import java.util.HashMap;
import java.util.Map;

/**
 * ========================
 * 统一响应结果集
 * User：Tanshu
 * Version: v1.0
 * ========================
 */
public class R extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    //int code; 操作代码
    // String result;  提示信息
    // T data;  结果数据
    // T msg;

    public R(ResultCode resultCode) {
        this.put("code", resultCode.code());
        this.put("result", resultCode.message());
    }

    public R() {
        this.put("code", ResultCode.SUCCESS.code());
        this.put("result", ResultCode.SUCCESS.message());
    }

    public R(ResultCode resultCode, Object data) {
        this.put("code", resultCode.code());
        this.put("result", resultCode.message());
        this.put("data", data);
    }

    public R(ResultCode resultCode, Object data, Object msg) {
        this.put("code", resultCode.code());
        this.put("result", resultCode.message());
        this.put("data", data);
        this.put("msg", msg);
    }

    public R(String result) {
        this.put("result", result);
    }

    public static R SUCCESS() {
        return new R(ResultCode.SUCCESS);
    }

    public static R ok() {
        return new R(ResultCode.SUCCESS);
    }

    public static <T> R SUCCESS(T data) {
        return new R(ResultCode.SUCCESS, data);
    }

    public static R FAIL() {
        return new R(ResultCode.FAIL);
    }

    public static R FAIL(String message) {
        return new R(ResultCode.FAIL).put("msg", message);
    }

    public static R Exception(String message) {
        return new R(ResultCode.EXCETPION).put("msg", message);
    }

    public int getCode() {
        return (int) this.get("code");
    }

    public void setCode(int code) {
        this.put("code", code);
    }

    public String getResult() {
        return this.get("result").toString();
    }

    public void setResult(String result) {
        this.put("result", result);
    }

    public Object getData() {
        return this.get("data");
    }

    /**
     * 利用fastjson进行逆转.
     * @param key
     * @param tTypeReference
     * @param <T>
     * @return
     */
    public <T> T getData(String key, TypeReference<T> tTypeReference){
        Object obj = get(key);
        String jsonString = JSON.toJSONString(obj);
        return JSON.parseObject(jsonString, tTypeReference);
    }


    public void setData(Object data) {
        this.put("data", data);
    }

    public R setDataR(Object data) {
        this.put("data", data);
        return this;
    }

    public Object getMsg() {
        return this.get("msg");
    }

    public void setMsg(Object msg) {
        this.put("msg", msg);
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R insert_ERROR() {
        return new R(ResultCode.FAIL, Constans.INSERT_ERR_MSG);
    }

    public static R update_ERROR() {
        return new R(ResultCode.FAIL, Constans.UPDATE_ERR_MSG);
    }

    public static R delete_ERROR() {
        return new R(ResultCode.FAIL, Constans.DELETE_ERR_MSG);
    }

}
